package com.doujiao.auth.controller;

import com.doujiao.core.Result;
import com.doujiao.auth.service.AdminService;
import com.doujiao.core.auth.dto.LoginDTO;
import com.doujiao.core.auth.entity.Admin;
import com.doujiao.core.auth.vo.LoginVO;
import com.doujiao.core.auth.dto.AdminQueryDTO;
import com.doujiao.core.auth.vo.AdminPageVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wuyunbin
 * @since 2025-05-24
 */
@Tag(name = "管理员管理")
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Resource
    private AdminService adminService;

    @Operation(summary = "管理员登录")
    @PostMapping("/login")
    public Result<LoginVO> login(@RequestBody LoginDTO loginDTO) {
        LoginVO loginVO = adminService.login(loginDTO);
        return Result.success(loginVO);
    }

    @Operation(summary = "新增管理员")
    @PostMapping
    public Result<Long> createAdmin(@RequestBody Admin admin) {
        Long adminId = adminService.createAdmin(admin);
        return Result.success(adminId);
    }

    @Operation(summary = "修改管理员信息")
    @PutMapping("/{id}")
    public Result<Boolean> updateAdmin(@PathVariable Long id, @RequestBody Admin admin) {
        admin.setAdminId(id);
        boolean success = adminService.updateAdmin(admin);
        return Result.success(success);
    }

    @Operation(summary = "启用/禁用管理员")
    @PatchMapping("/{id}/status")
    public Result<Boolean> updateAdminStatus(@PathVariable Long id, @RequestParam Boolean disabled) {
        boolean success = adminService.updateAdminStatus(id, disabled);
        return Result.success(success);
    }

    @Operation(summary = "分页查询管理员")
    @PostMapping("queryAdmins")
    public Result<AdminPageVO<Admin>> queryAdmins(@RequestBody AdminQueryDTO queryDTO) {
        AdminPageVO<Admin> pageVO = adminService.queryAdmins(queryDTO);
        return Result.success(pageVO);
    }
}
